home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / vidhandl / monta.cpp < prev    next >
C/C++ Source or Header  |  1999-02-03  |  4KB  |  142 lines

  1. //Copyright (c) By Márcio Afonso Arimura Fialho
  2. //Freeware version, may be distributed freely
  3.  
  4. #include <conio.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. #include "readname.cpp"
  9.     //readfname defined in readname.cpp appends a extension to a filename if
  10.     //none is given
  11.  
  12. void main (int n, char *ent[16])
  13.  {
  14.     FILE *source, *target;
  15.     char targetpath[132];
  16.     char *bin_open="\x0d\x0a//\1"; //This is the file open tag in binary format
  17.     char *bin_close="//\2\x0d\x0a"; //This is the file close tag in binary format
  18.     char *text_open="\x0d\x0a//<FILE=\""; //This is the file open tag in ASCII format
  19.     char *text_close="\x0d\x0a//</FILE>\x0d\x0a"; //This is the file close tag in ASCII format
  20.     int format=1; //if format==0 uses binary format.
  21.     int c0; //counter
  22.     int a0; //auxiliar var
  23.     int key; //keyboard input
  24.  
  25.     if (n<2)
  26.      {
  27.         printf ("\
  28. MONTA 2.0b - Simple text file grouper for C/C++ sources - Freeware version\n\n\
  29. Usage:\tMONTA [/B] (archive name [.SRC]) file1 [file2 [...]]\twhere:\n\
  30. \t/B\t\tcreate archive using binary file tags\n\
  31. \tarchive name\tname of the archive to be created\n\t\t\t(.SRC extension supplied if none is supplied)\n\
  32. \tfile1, file2\tfiles to be stored\
  33. \n\n\
  34. By Márcio A. A. Fialho\nhttp:pessoal.iconet.com.br/jlfialho\n\
  35. e-mail:jlfialho@iconet.com.br OR jlfialho@yahoo.com");
  36.         return;
  37.      }
  38.     if (n==2)
  39.      {
  40.         puts ("ERROR: NO file to include in archive \nType MONTA with no parameters to obtain help.\a");
  41.         return;
  42.      }
  43.     c0=0;
  44.     a0=1;
  45.     if (*ent[1]=='/') //verifies if /B swich is on
  46.      {
  47.         ent[1]++;
  48.         if (*ent[1]=='B' || *ent[1]=='b')
  49.             format=0;
  50.         a0++;
  51.      }
  52.  
  53.     readfname(targetpath,ent[a0],".SRC");
  54.         //reads filename and appends .SRC extension if none is found
  55.  
  56.     key='O'; //if key==O, the archive will be written from the begin
  57.     target=fopen(targetpath,"rb");
  58.     if (target!=NULL)
  59.     //if archive (target file) already exists, prompts for action
  60.      {
  61.         fclose (target);
  62.         for(;;)
  63.          {
  64.             printf ("\nFile already exist. Overwrite/Append/Quit?(O/A/Q)");
  65.             key=getche ();
  66.             if (key>0x60 && key<0x7b) //upcases the key returned by the user
  67.                 key=key-0x20;
  68.             if (key=='O')
  69.              {
  70.                 target=fopen (targetpath,"wb");
  71.                 break;
  72.              }
  73.             else if (key=='A')
  74.              {
  75.                 target=fopen (targetpath,"rb+");
  76.                 fseek(target,0,2);
  77.                 break;
  78.              }
  79.             else if (key=='Q')
  80.              {
  81.                 fclose(target);
  82.                 return;
  83.              }
  84.          }
  85.      }
  86.      else
  87.         target=fopen(targetpath,"wb");
  88.  
  89.     if (target==NULL)
  90.      {
  91.         printf ("ERROR: Couldn't open target file for writing.");
  92.         fcloseall();
  93.         return;
  94.      }
  95.  
  96.     if (key=='O') //writes message
  97.         fputs("//* * * Archive created by MONTA utility * * * \x0d\x0a",target);
  98.  
  99.     a0++;
  100.     for(c0=a0;c0<n;c0++) //stores files to be stored
  101.      {
  102.         source=fopen(ent[c0],"rb");
  103.         if (source==NULL)
  104.          {
  105.             printf ("\nWarning: file \"%s\" couldn't be opened. Skipping file \"%s\"\n",ent[c0],ent[c0]);
  106.             goto jmp1;
  107.          }
  108.          else
  109.             printf ("\n%s",ent[c0]);
  110.         if (format==1)
  111.          {
  112.             fputs(text_open,target);
  113.             fputs(ent[c0],target);
  114.             fputs("\">\x0d\x0a",target);
  115.          }
  116.          else
  117.          {
  118.             fputs(bin_open,target);
  119.             fputs(ent[c0],target);
  120.             fputs("\x0d\x0a",target);
  121.          }
  122.         while (!(feof(source))) //writes the files to be stored in the archive
  123.          {
  124.             fputc ((char)fgetc(source),target);
  125.          }
  126.         fseek (target,-1,2);
  127.         if (format==1)
  128.             fputs(text_close,target);
  129.          else
  130.             fputs(bin_close,target);
  131.         fclose (source);
  132.       jmp1:
  133.      }
  134.   pula1:
  135.     fclose(source);
  136.     fclose(target);
  137.  }
  138.  
  139. //by Marcio A. A. Fialho
  140. //http: pessoal.iconet.com.br/jlfialho/english.htm
  141. //E-mail : jlfialho@iconet.com.br (main e-mail) OR
  142. //    (alternate e-mail) jlfialho@yahoo.com